var app = getApp();
const pathUrl = app.globalData.pathUrl;
Page({
  data: {
    pathUrl: pathUrl,
    isSubmit: true, //防止重复提交


  },
  onLoad: function (e) {
    var openId = wx.getStorageSync('openId');
    var proNo = e.proNo;
    var proName = e.proName;
    var orderNo = e.orderNo;
    var type = e.type;
    if(type =='zjr'){
      this.setData({
        proNo: proNo,
        openId: openId,
        orderNo: orderNo,
        type: type
      })
    }
    if(type=='dbr'){
      var nameNumber = e.nameNumber;
      this.setData({
        proNo: proNo,
        openId: openId,
        orderNo: orderNo,
        type: type,
        nameNumber: nameNumber
      })
    }
   
    wx.setNavigationBarTitle({
      title: proName
    })
    console.log("proNo:" + proNo)
    this.getProAttList();
    // this.getPicUrl();

  },
  getPicUrl:function(e){
    var that = this;
    wx.request({
      url: pathUrl + '/rest/hrzxjj03Controller/list/'+that.data.openId,
      data:{
        pageNumber:'1',
        pageSize:'10',
       fxjOut01:that.data.orderNo,
        // fxjOut02: that.data.type,
      },
      method:'GET',
      success:function(e){
        console.log(e)
        that.setData({
          proAttList:e.data.dalta
        })
      }
    })
  },
  orderBy9Input: function (e) {
    this.setData({
      orderBy9: e.detail.value
    })
  },

  orderBy5Input: function (e) {
    this.setData({
      orderBy5: e.detail.value
    })
  },
  
  //获取上传附件列表
  getProAttList: function () {
    var that = this;
    wx.request({
      url: pathUrl + '/rest/fxjProMainController/' + that.data.proNo,
      success: function (res) {
        console.log(res.data)
        if (res.data.ok || res.data.data) {
          var newProAttList = res.data.data.fxjProAttList;
          for (var i = 0; i < newProAttList.length; i++) {
            newProAttList[i].picList = [];
          }
          that.setData({
            proAttList: newProAttList,
            proIsdb: res.data.data.proIsdb
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '获取数据失败',
            confirmColor: '#7aa6d1',
            showCancel: false
          })
        }
      }
    })
  },
  //上传附件图片
  getEnclosurePic: function (e) {
    console.log(e);
    var picIndex = e.currentTarget.dataset.index;
    var attType = e.currentTarget.dataset.atttype;
    var picCode = e.currentTarget.dataset.code;
    var that = this;
    console.log(that.data.proAttList);

    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths;
        //启动上传等待中...  
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        var uploadImgCount = 0;
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          wx.uploadFile({
            url: pathUrl + '/rest/base/upload',
            filePath: tempFilePaths[i],
            name: 'uploadfile_ant',
            formData: {
              sbtype: 'NO',
              filetype: "sb",
              orderNo: that.data.orderNo
            },
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function (res) {
              console.log(res)
              uploadImgCount++;
              var data = JSON.parse(res.data);
              var proAttListInfo = that.data.proAttList;
              console.log(that.data.proAttList);
              console.log('bbbbb');
              console.log(JSON.parse(res.data));
              console.log(data)
              console.log(data[0])
              var goods = data[0]
              console.log(goods);
              console.log('dsaghdjbashjdbhajbdhj')
              console.log(picIndex)
              console.log(proAttListInfo)
              proAttListInfo[picIndex].picList.push({
                picUrl: goods.fileUrl,
                picId: goods.fileId
              });
              console.log('ssss')
              that.setData({
                proAttList: proAttListInfo,
                picId: goods.fileId,
                picCode: picCode,
                picFileName: goods.fileName,
                picAddress: goods.fileUrl
              });
              if(that.data.type=='zjr'){
                var data = {
                  createBy: that.data.openId,
                  fxjOut01: that.data.orderNo, //订单编号
                  picId: that.data.picId,
                  picCode: that.data.picCode,
                  picFileName: that.data.picFileName,
                  picAddress: that.data.picAddress,
                  fxjOut02: that.data.type,
                };
              }
              if (that.data.type == 'dbr') {
                var data = {
                  createBy: that.data.openId,
                  fxjOut01: that.data.orderNo, //订单编号
                  picId: that.data.picId,
                  picCode: that.data.picCode,
                  picFileName: that.data.picFileName,
                  picAddress: that.data.picAddress,
                  fxjOut02: that.data.type,
                  fxjOut03: that.data.nameNumber,

                };
              }
              wx.request({
                url: pathUrl + '/rest/hrzxjj03Controller',
                data: data,
                method: 'POST',
                success: function (res) {
                    console.log(res)
                    wx.showModal({
                      title: '提示',
                      content: '上传成功',
                      showCancel:false
                    })
                }
              })
              if (uploadImgCount == tempFilePaths.length) {
                wx.hideToast();
              }
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false
              })
            }
          });
        }
      }
    });
  },
  //删除上传附件图片
  deletePic: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var imgIdx = e.currentTarget.dataset.imgIdx;
    var deletPicId = that.data.proAttList[index].picList[imgIdx].picId;
    if (that.data.flag) {
      that.data.proAttList[index].picList.splice(imgIdx, 1);
      that.setData({
        proAttList: that.data.proAttList,
        flag: false
      })
    }
    wx.request({
      url: pathUrl + '/rest/base/delfile/' + deletPicId,
      success: function (res) {
        if (res.data.ok || res.data.data) {
          that.setData({
            flag: true
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '获取数据失败',
            confirmColor: '#7aa6d1',
            showCancel: false
          })
        }
      }
    })
  },
  submitBtn: function () {
    var that = this;
    // if (that.data.isSubmit) {
    //   that.setData({
    //     isSubmit: false
    //   });
      var data = {
        createBy: that.data.openId,
        fxjOut01: that.data.orderNo, //订单编号
        picId: that.data.picId,
        picCode: that.data.picCode,
        picFileName: that.data.picFileName,
        picAddress: that.data.picAddress,
        fxjOut02: that.data.type, //订单编号

      };
      console.log("data:" + JSON.stringify(data));
      wx.request({
        url: pathUrl + '/rest/hrzxjj03Controller' ,
        data: data,
        method: 'POST',
        success: function (res) {
          console.log(res.data)
          // if (res.data.ok) {
          //   that.setData({
          //     isSubmit: true
          //   });
            wx.showToast({
              title: '提交成功',
              duration: 1000,
              mask: true,
              success: function () {
                setTimeout(function () {
                  // wx.switchTab({
                  //     url: "/pages/index/index"
                  // })
                  wx.navigateBack({
                    dalta: 1
                  })
                }, 1000);
              }
            })
          // } else {
          //   that.setData({
          //     isSubmit: true
          //   });
          //   wx.showToast({
          //     title: '提交失败',
          //     icon: 'none',
          //     mask: true,
          //     duration: 1000
          //   })
          // }
        }
      })
    // } else {
    //   wx.showToast({
    //     title: '请勿重复提交',
    //     icon: 'none',
    //     mask: true,
    //     duration: 1000
    //   })
    // }
  },

  submitBtn1:function(){
    wx.navigateBack({
      delta: 1

    })
  },
  // downFile: function() {
  //     wx.downloadFile({
  //         url: 'https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/zxsqs.docx',
  //         header: {},
  //         success: function(res) {
  //             console.log(res)
  //             var filePath = res.tempFilePath;
  //             wx.openDocument({
  //                 filePath: filePath,
  //                 fileType: 'docx',
  //                 success: function (res) {
  //                     console.log('打开文档成功')
  //                 }
  //             })
  //         },
  //         fail: function(res) {
  //             wx.showToast({
  //                 title: '下载出错',
  //                 icon: 'none',
  //                 mask: true,
  //                 duration: 10000
  //             })
  //         }
  //     })
  // },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})